package com.iweb.shopping.dao;

import com.iweb.shopping.model.Address_sh;
import com.iweb.shopping.model.Cart;
import com.iweb.shopping.model.Goods;
import com.iweb.shopping.model.Goodsdetail;
import com.iweb.util.JdbcUtil;
import com.iweb.util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShoppingDaoImpl implements ShoppingDao{
    @Override
    public List<Goods> selectByPage(PageModel pageModel,Integer userid) {
        List<Goods> goods = new ArrayList<>();
        String likeValue = pageModel.getLikeValue();
        StringBuffer sql = new StringBuffer("select tc.cartid,tg.goodsid,tg.goodsname,tg.goodsprice from tb_goods tg,tb_cart tc where tg.goodsid=tc.goodsid and tc.userid=? and (tg.goodsid like ? or tg.goodsname like ? or tg.goodsprice like ?)");
        int recordCount = JdbcUtil.selectBySql(sql.toString(),userid,"%"+likeValue+"%","%"+likeValue+"%","%"+likeValue+"%").size();
        pageModel.setRecordCount(recordCount);//将查询后的总记录数赋给原本的总记录数
        sql.append(" order by tg.goodsid desc limit ?,?");
        int starNo = pageModel.getPageSize()*(pageModel.getPageNo()-1);
        List<Map<String,Object>> list =  JdbcUtil.selectBySql(sql.toString(),userid,"%"+likeValue+"%","%"+likeValue+"%","%"+likeValue+"%",starNo,pageModel.getPageSize());
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            Integer cartid = (Integer) map.get("cartid");
            Integer goodsid = (Integer) map.get("goodsid");
            String goodsname = (String) map.get("goodsname");
            String goodsprice = (String) map.get("goodsprice");
            goods.add(new Goods(cartid,goodsid,goodsname,goodsprice));
        }
        return goods;
    }

    @Override
    public Goodsdetail selectById(Integer goodsid) {
        String sql ="select tg.goodsid,tg.goodsname,tg.goodsprice,date_format(tg.createtime,'%Y-%m-%d %H:%i:%s') createtime,tg.description,tu.userfullname,tu.phone from tb_goods tg,tb_user tu where tg.userid=tu.userid and tg.goodsid = ?";
        List<Map<String,Object>> list = JdbcUtil.selectBySql(sql,goodsid);
        Map<String,Object> map = list.get(0);
        String goodsname = (String) map.get("goodsname");
        String goodsprice = (String) map.get("goodsprice");
        String createtime = (String) map.get("createtime");
        String description = (String) map.get("description");
        String userfullname = (String) map.get("userfullname");
        String phone = (String) map.get("phone");
        Goodsdetail goodsdetail= new Goodsdetail(goodsid,goodsname,goodsprice,createtime,userfullname,phone,description);
        return goodsdetail;
    }

    @Override
    public List<Address_sh> queryAddress(Integer userid) {
        List<Address_sh> address_shs =new ArrayList<>();
        String sql = "select ta.addressid,tu.username,ta.phone,ta.address from tb_user tu,tb_address ta where tu.userid=ta.userid and ta.userid=? ";
        List<Map<String,Object>> list = JdbcUtil.selectBySql(sql,userid);
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            Integer addressid =(Integer) map.get("addressid");
            String username = (String) map.get("username");
            String phone = (String) map.get("phone");
            String address = (String) map.get("address");
            address_shs.add(new Address_sh(addressid,username,phone,address));
        }
        return address_shs;
    }

    @Override
    public Address_sh queryAddressById(Integer addressid) {
        String sql ="select tu.username,ta.phone,ta.address from tb_user tu,tb_address ta where tu.userid=ta.userid and ta.addressid=? ";
        List<Map<String,Object>> list = JdbcUtil.selectBySql(sql,addressid);
        Map<String,Object> map = list.get(0);
        String username = (String) map.get("username");
        String phone = (String) map.get("phone");
        String address = (String) map.get("address");
        Address_sh address_sh= new Address_sh(addressid,username,phone,address);
        return address_sh;
    }

    @Override
    public int deleteOne(Integer cartid) {
        String sql = "delete from tb_cart where cartid=?";
        return JdbcUtil.editBySql(sql,cartid);
    }

    @Override
    public int saveOne(Cart cart) {
        String sql = "insert into tb_cart values(null,?,?,now())";
        return JdbcUtil.editBySql(sql,cart.getGoodsid(),cart.getUserid());
    }
}
